package com.example.ordersystem.utils;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ordersystem.MainActivity;
import com.example.ordersystem.R;
import com.example.ordersystem.pojo.Food;
import com.example.ordersystem.pojo.Store;

import java.util.List;

/*
 * 该适配器是为了展示订单菜品列表（listView）
 */
public class OrderFoodAdapter extends ArrayAdapter {
    private final int resourceId;


    public OrderFoodAdapter(Context context, int textViewResourceId, List<Food> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Food food = (Food) getItem(position); // 获取当前项的Food实例

        final View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
        ImageView foodImage = view.findViewById(R.id.food_image2);//获取该布局内的图片视图
        TextView foodName = view.findViewById(R.id.food_name2);
        TextView foodMessage = view.findViewById(R.id.food_message2);
        TextView storeName = view.findViewById(R.id.store_name2);
        TextView foodprice = view.findViewById(R.id.food_price2);
        TextView foodNum = view.findViewById(R.id.food_num);//菜品数量
        Button btn_my_order = view.findViewById(R.id.btn_my_order);
        String storeName1 = null;
        foodImage.setImageResource(food.getImageId());

        List<Store> storeList = ListDataSave.getStoreList(getContext());
        for (Store store : storeList) {
            if (food.getStoreId() == store.getStoreId()) {
                storeName1 = store.getStoreName();
            }
        }
        foodImage.setImageResource(food.getImageId());//为图片视图设置图片资源
        storeName.setText("店铺名：" + storeName1);
        foodName.setText("菜品名：" + food.getFoodName());
        foodMessage.setText("菜品简介：" + food.getMessage());
        foodprice.setText("菜品价格：" + food.getPrice()*food.getNum());
        foodNum.setText("数量：" + food.getNum());

        btn_my_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Order_Food.deleteOrderFood(food);
                view.setVisibility(View.GONE);
                Toast.makeText(getContext(), "已移除该订单", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), MainActivity.class);
                boolean order = true;
                intent.putExtra("order", order);
                getContext().startActivity(intent);
            }
        });
        return view;
    }

}
